var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['附近的人', '附近动态'],
    tabIndex: 0,
    page:0
  },
  setTab: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex: index
    })
  },
  follow: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.follow_openid = e.currentTarget.dataset.openid;
    app.ihttpPost('https://www.58zltc.com/api.php/Quan/follow', data, function (res) {
      if (res.data.err_code === 0) {
        wx.showToast({
          title: "成功"
        })
        setTimeout(function () {
          wx.hideLoading()
          that.onLoad()
        }, 1200)

      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    wx.showLoading({
      title: '加载中…',
    })
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/giftList', function (res) {
      console.log(res)
      that.setData({
        gift_list: res.data
      })
    });
    wx.getLocation({
      type: 'wgs84',
      complete: function (resq) {
        that.setData({
          laNum: resq.latitude,
          loNum: resq.longitude
        })
        wx.request({
          url: 'https://www.58zltc.com/api.php/Quan/fujin?token=' + that.data.token + '&la=' + resq.latitude + '&lo=' + resq.longitude + '&openid=' + app.globalData.openid + '&page=' + that.data.page,
          success: res => {
            that.setData({
              user_list: res.data,
              page: that.data.page + 1,
            })
            wx.hideLoading();
          }
        })
      }
    })


    // app.ihttpGet('https://www.58zltc.com/api.php/Quan/fujin', function (res) {
    //   that.setData({
    //     user_list: res.data
    //   })
    // });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/index/openid/' + app.globalData.openid + '/pageNum/' + that.data.page, function (res) {
      that.setData({
        list: res.data
      })
      wx.hideLoading();
    });
  },
  backto: function (e) {
    wx.navigateBack({

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var t = this;
    this.setData({
      page: t.data.page + 1,
    })
    this.onLoad();
    setTimeout(function () {
      wx.stopPullDownRefresh()
      wx.showToast({
        title: '已刷新',
      })
    }, 600)
  },
  goDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(e.currentTarget.dataset.video);
    if (e.currentTarget.dataset.video) {
      var url = '/pages/quan/video/video?id=' + id;
    } else {
      var url = '/pages/quan/detail?id=' + id;
    }
    wx.navigateTo({
      url: url,
    })
  },
  goMember: function (e) {
    var openid = e.currentTarget.dataset.openid;
    var url = '/pages/quan/member/member?openid=' + openid;
    wx.navigateTo({
      url: url,
    })
  },
  gift: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      is_gift: true,
      gift_id: id
    })
  },
  hiddenGift: function (e) {
    this.setData({
      is_gift: false
    })
  },
  setGift: function (e) {
    var idx = e.currentTarget.dataset.idx;
    let that = this;
    var gift_list = that.data.gift_list;
    gift_list.forEach(function (value, key) {
      gift_list[key].active = key == idx ? true : false;
    })
    this.setData({
      gift_list: gift_list,
      active_gift: gift_list[idx]
    })
  },
  payGift: function (e) {
    let that = this;
    var data = this.data;
    var post_data = {};

    data.list.forEach(function (value, idx) {
      if (value.id == data.gift_id) {
        post_data.to = value.follow_openid;
      }
    })
    post_data.type = 'quan';
    post_data.from = app.globalData.openid;
    post_data.id = data.gift_id;
    post_data.gift_id = data.active_gift.id;

    app.ihttpPost('https://www.58zltc.com/api.php/Quan/payGift', post_data, function (res) {
      if (res.data.err_code == 400) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        var data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '赠送成功！',
              })
            }
          }
        })
        that.reBg();
      }
    })
  },
  zan: function (e) {
   
    var id = e.currentTarget.dataset.id;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    var list = this.data.list;
    var that = this;
    wx.request({
      url: 'https://www.58zltc.com/api.php/Quan/zan?openid=' + app.globalData.openid + '&id=' + id + '&token=' + extConfig.appid,
      success: res => {
        if (res.data.err_code === 0) {
          list.forEach(function (vo, idx) {
            if (vo.id == id) {
              if (res.data.err_msg == '-1') {
                list[idx].zan_num = parseInt(list[idx].zan_num) - 1;
                list[idx].is_zan = false;
              } else {
                list[idx].zan_num = parseInt(list[idx].zan_num) + 1;
                list[idx].is_zan = true;
              }
            }
          })
          that.setData({
            list: list
          })
         
        } else {
          wx.showToast({
            title: res.data.err_msg
          })
        }
      }
    })
  },

  showImgs: function (e) {
    const pics = e.currentTarget.dataset.pics;
    const first_pics = e.currentTarget.dataset.image;
    wx.previewImage({
      urls: pics,
      current: first_pics
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})